from pyfeasc.saasc import *


# 使用示例
def main():
	
	# 读取数据
	adata = sc.read_h5ad("../data/input/anndata/AML_GSE154109.h5ad")
	adata.var_names_make_unique()
	adata.obs['celltype'] = adata.obs['Celltype (major-lineage)'].astype('category')
	
	# 计算基因频率
	gs = read_gmt('../data/input/marker/CD8_Tcells_activation.gmt')
	grate = get_gene_rate(background_geneset='../data/input/marker/Tres.kegg', signature_geneset=gs, mode="multiple")
	print("打印 grate 的前 5 行数据：\n", grate.head())
	
	# 降维重构, 计算 Tcell_activation 响应值, 计算细胞因子信号通路活性
	adata = rebuild_matrix(adata, method=['pca'], n_dim=16)
	response_data = compute_response(adata, gene_rate=grate, celltype="CD8T", signature="Tcell_activation")
	print("打印 response_data 的前 5 行数据：\n", response_data.head())
	
	signaling_data = compute_signaling(adata, model_file="../data/input/cytokine/signature.centroid.expand", celltype="CD8T",
	                                   cytokine=None, lambd=10000, num_permutations=0, test_method="two-sided")
	print("打印 signaling_data 的前 5 行数据：\n", signaling_data.head())
	
	res = pd.concat([response_data, signaling_data[["TGFB1"]]], axis=1)
	plot_response_signaling({'none': res})
	
	# Wnt 信号通路活性
	columns = read_tsv_header('../data/input/cytokine/SPEED2_signaling.tsv')
	print(columns)
	Signaling = compute_signaling(adata, model_file="../data/input/cytokine/SPEED2_signaling.tsv", celltype=None,
	                              cytokine=None, lambd=10000, num_permutations=0, test_method="two-sided")
	plot_activity(Signaling, adata)
	
	"""
	交互作用分析和 Tres 得分计算
	"""
	# 交互作用分析
	interaction_results = do_interaction(
			adata=adata,
			response_data=response_data,
			signaling_data=signaling_data,
			signature=['Tcell_activation'],
			cytokine=['TGFB1'],
			threshold=10
	)
	print("打印 interaction_results 的前5行数据：\n", interaction_results.head())
	
	# 计算 Tres 分数
	tres_genes = get_tres_signature(
			interaction_dataset=interaction_results,
			signature_cytokine=['TGFB1'],
			qvalue=0.05,
			cutoff=0.5,
			method='median'
	)
	print("打印 tres_genes 的前 5 行数据：\n", tres_genes.head())


if __name__ == '__main__':
	main()
